'use strict';

function PhonemapLocation() {
	 if (!(this instanceof PhonemapLocation)) return new PhonemapLocation();
	
	 PhonemapLocation.prototype.doGeolocation = function() {
			if (navigator.geolocation) {
				navigator.geolocation
						.getCurrentPosition(positionSuccess, positionError);
			} else {
				positionError(-1);
			}
		};
}

var isMobile = {
	Android : function() {
		return navigator.userAgent.match(/Android/i);
	},
	BlackBerry : function() {
		return navigator.userAgent.match(/BlackBerry/i);
	},
	iOS : function() {
		return navigator.userAgent.match(/iPhone|iPad|iPod/i);
	},
	Opera : function() {
		return navigator.userAgent.match(/Opera Mini/i);
	},
	Windows : function() {
		return navigator.userAgent.match(/IEMobile/i);
	},
	any : function() {
		return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS()
				|| isMobile.Opera() || isMobile.Windows());
	}
};




function positionError(err) {
	var msg;
	switch (err.code) {
	case err.UNKNOWN_ERROR:
		msg = "Unable to find your location";
		break;
	case err.PERMISSION_DENINED:
		msg = "Permission denied in finding your location";
		break;
	case err.POSITION_UNAVAILABLE:
		msg = "Your location is currently unknown";
		break;
	case err.BREAK:
		msg = "Attempt to find location took too long";
		break;
	default:
		msg = "Location detection not supported in browser";
	}
	document.getElementById('info').innerHTML = msg;
}

var shapeDickens = {
	coord : [ 85, 7, 130, 26, 148, 61, 221, 60, 221, 100, 152, 100, 129, 132,
			82, 156, 40, 137, 17, 113, 11, 79, 24, 45, 44, 20 ],
	type : 'poly'
};

function positionSuccess(position) {
	// Centre the map on the new location
	var coords = position.coords || position.coordinate || position;
	var latLng = new google.maps.LatLng(coords.latitude, coords.longitude);
	map.setCenter(latLng);
	map.setZoom(12);
	var marker = new google.maps.Marker({
		map : map,
		position : latLng,
		title : 'Why, there you are!',
		zIndex : 902,
		shape : shapeDickens
	});
	document.getElementById('info').innerHTML = 'Looking for <b>'
			+ coords.latitude + ', ' + coords.longitude + '</b>...';

	// And reverse geocode.
	(new google.maps.Geocoder()).geocode({
		latLng : latLng
	}, function(resp) {
		var place = "You're around here somewhere!";
		if (resp[0]) {
			var bits = [];
			for (var i = 0, I = resp[0].address_components.length; i < I; ++i) {
				var component = resp[0].address_components[i];
				if (contains(component.types, 'political')) {
					bits.push('<b>' + component.long_name + '</b>');
				}
			}
			if (bits.length) {
				place = bits.join(' > ');
			}
			marker.setTitle(resp[0].formatted_address);
		}
		document.getElementById('info').innerHTML = place;
	});
}
//
//function contains(array, item) {
//	for (var i = 0, I = array.length; i < I; ++i) {
//		if (array[i] == item)
//			return true;
//	}
//	return false;
//}

//function getlocation() {
//	if (navigator.geolocation) {
//		navigator.geolocation.getCurrentPosition(positionSuccess1,
//				positionError);
//	} else {
//		positionError(-1);
//	}
//}

//var oldPos = null;

/**
 * Position Success.
 * 
 * @param position
 */
//function positionSuccess1(position) {
//	// Centre the map on the new location
//	var coords = position.coords || position.coordinate || position;
//	var latLng = new google.maps.LatLng(coords.latitude, coords.longitude);
//	var curpos = latLng;
//
//	if (oldPos != null) {
//		// dist = distance(oldPos.lat(), oldPos.lng(), curpos.lat(),
//		// curpos.lng(), 'K');
//		oldPos = curpos;
//		alert('Location: ' + curpos.toString());
//	} else {
//		oldPos = curpos;
//	}
//
//}

//var listener = setInterval(function() {
//	getlocation()
//}, 500000);


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
if (!window.console) window.console = { log: function() {} };